refactoring the first working version