fixing various bugs in the code