Added event listener for onReplaced.

There are quite a few changes and bug fixes here.  Let me know if you have any questions about them.