Simplify code

Reduce indentation by using early returns and removing superfluous ifs.