Slight peformance improvements and code clean up. Added comments.