Refactored and fixed tests