Replace few magic numbers with macros and some confusing function names