Refactor and style cleanup