Bugfixes & code cleanup