Code Cleanup and Bugfixing

Generally commenting and cleainung up the code as well as fixing some
smaller bugs