Refactor and clean up