Misc bugfixes & cleanup