cleaned up code by breaking it into and reusing smaller methods