Refactor preprocessing