refactor and clean-up of the code