inserted some debugging and fixed bug