Exception handling and error checking added