cleanup and integrate @sstuckis work