Do some major refactoring