cleaned up code and added test