one big commit with refactoring, rebasing would be impossible

sorry for the huge commit, but it's pretty solid.