Optimize conditionals and reduce lookups.
Also, make the code easier to read removing unneded indentations.