The idea is generally right, for finding out why it does not work, check the whole code and debug it with the watch function. Also read the troubleshooting page in the manual.