Reduce if statement and computational time