Almost total refactor