Cleaned it up and added comments