Bugfixes

WIP bug fixes and cleanup