Improve environment variable behaviour. TODO: document, test