Many bug fixes and code review