Cleaned up code and added comments for easy reference in the future