Functional version for small inputs. Details in the code should be organized and execute further test with bigger inputs