This function is duplicated on line 323. One should be moved to the top of the file.