Made more generic so function can be reused.