Cleaned up the code and used Object Oriented Javascript