All of this stuff should probably be in it's own file/class. It's not that complex now, but it's likely to get more complex, and it's functionality is totally unrelated to anything else that world is doing.