Bug fixes and added asserts