Add unit testing -- separate logic from app layer

Pull out some concerns for unit testing and clean a couple minor things