Working version (all tests passed)