Moving conditions to function