cleaned up the code and added proper comments