Cleaned up slightly and added some comments