Refactor and fix erroring