Cleaned up the code, added comments for all the functions