some minor refactoring
more refactoring

and fixed bug in previous commit