some improvements and corrections
smartened up by xpertrule