Code fixes and cleanup