some refactoring and restructuring
refactoring missing from last commit