updated some of the functions to be cleaner (and potentially faster).