minor changes and added error handling