reorder code to remove not needed computation in some cases.