rewrote it all to functional paradigm