first worked version after total refactor