move assertion to top of function. Found by PVS-Studio