Refactoring

Removed direct access to SDL structures and moved functions around to
get them out of the global environment.