Some clean up and minor bugfixes