code refactoring towards hopefully a working version