cleaned up the code as well as added more comments