removed some unnecessary intermediary variables
added braces for ifs