first working version after major code refactor