Refactored loading stuff into middleware

Easier to load things and handle errors
fix whitespace