Exception handling and bug fix