Code fully tested on the simulator