more refact and unit tests