Refactor to fix several lines of logic