some moderate refactoring