cleaned up code and removed duplicate code