large code refactor