Refactored functions for faster performance June 8