fixed some size issues and improved error control