Rearrange functions to avoind unnecessary comparisons while pattern matching

Signed-off-by: Peter Lemenkov <lemenkov@gmail.com>