Exception handling and minor cleanup