Code cleanup

I've done some code cleanup, basically to break up long methods into smaller ones, and to tidy up small details.

This PR also fixes the Travis and Gemnasium badges, and adds another one for Code Climate.