Cleaned up a bit and added a couple of comments