Final, working version with all tests