updated comments and slightly more error handling