Reducing function calls and if statements