added error handling, removed comments, extra if-else