Test refactored and bugs fixed