working version, passed all tests