Refactor and fix