distilled code

code has been reworked on to prepare it for proper testing