code refactored

refactored code.added more debugging statements