WIP seems to work, needs refactoring and tests