lots of refactoring. more to come