Fixed error and refactored test function