Fixes and refactoring to tests