initial refactoring, broken, don't use! use 'safe' branch