Removed unnecessary code.
Encapsulated some duplicate code.