Code Improvements

Hi Tom,

I was using this in a project and noticed that the code only works when the array controllerCode is empty when you start entering the code. So I fixed this in commit 39c716b400ce543fc74082dc6536da387296f841.

While I was in the code I noticed that some performance improvements could be made which are covered by the rest of the commits.

It would be great if you could merge these changes into your branch :)

Many Thanks
Stephen