cleaned up the code, added comments