More test improvement and code cleanup