Fixing jslint errors

Defining functions at the top in order to avoid "was used before it was
defined" errors.