Cleaned up coded and added some comments