Bug fixes and proper exception handling