refactor and move code around