Refactor for better organization and fix bug in find/replace functions