better organized code. Refactored out duplicated code.