Functional working version - tested
...