added some errorchecking; fixed indentation; added support for 4000count mode