fixed algorithms when regenerating/loading maps
MAJOR cleanup, hopefully nothing got broken...