Added more error checking, cleaned up the code