Bug fixes and better exceptions