Refactor and fix all around