Big refactor, probably broken everything :(