Improved "safe teleport" function.