Cleanup and bugfixes

This is a general cleanup for better standards complicance plus some bugfixes