complete refactoring (not finished yet)