Lots of refactoring of code