Moved SELECT statement for goal to top and added error checking