cleaned up, commented, added error handling