cleaned up a bit and added comments