massiven refactoring for some reason