Refactor quite a bit