Fix for caret positioning on applying a new mask and adding characters before a mask character

This patch should fix issue #141 
It also addresses issue #136, although is does not solve exact issue described