Removed test code and added error handling