Implemented error handling and some minor changes