Respect enviromental variables, fix various bugs.