updated functions to improve speed