Tried to refactor things a bit

... but rewriting doesn't seem to work as well as it should