code re-arrangement for clarity