Simplified a bit and removed the internal function