Cleaned up code and wrote additional comments