Implement terrainFunction, add unit tests