major re-factor of code