Some cleanup and minor bugfixes