Further refactor, and switch to using LastIndex for best compatibility