Bug fixes

Reorder ifs