Added comments and cleaned up some code

Mostly the source of open source snippets i used.