Mostly code refactoring