started refactoring, but in very broken state right now - easy to fix later