bug fixes and fixes to various functions