defined invariant and added explicit returns