Cleaned up the function a bit.