Redesigned the temp function, runs more effeciently