Cleaned up the code and documented fucntions.