Completed recursive function for schedule gen