almost working version
refactor of the code