Bug fixes

Bug fixes and correction to the code