Updated to more efficient functions.