Refactor remaining code