Step 6 and major refactoring