Code we ended up with