First working version (i.e. passes all tests)