Start refactoring. Not finished yet.