Refactor for sanity and performance