Added algorithm based off of Doom Builder 2's code.