Refactoring complete. Now works with transformed terrains too.