Some bug fixes and improvements

Hi,

Fixed a few minor bugs and improved the stability of the code.

regards
peter