Large re-write to prevent frame conflicts and simplify code