codecleanup, optimized, debug prints added