cleaned up the code and tested to work