Bug fixes

Hi,

I've fixed several bugs and improved the code. 

Thanks!

Sachin