Found a bug related to environment, fixed some order issues in a set of if statements