final version without refactoring