Fix crash + refactor