Refactored test function