Cleanup of code and fixes