Code refactored, my initial version