Apply code review comments and fix tests