fixed test and some minor refactoring