Cleaned up and commented the code.