Cleaned up and documented the code