added tests and fixed some while cond. bugs