refactoring/reimplementation WIP. uncommented relevant unit tests