Refactor: moved logic into methods

Extracted behaviors into small methods to allow for easier extension and
testing.