Minor bug fixes, and better exception handling