get_my_courses always returns 2 more courses than specified in $limit (if available).
This is because of the way that it applied the limit.
It does it using a counter as it steps through the courses. In this process, there are two off by one error.
I have attached a diff that simplifies it somewhat (to read), and removes the two off by one errors.