Further bug fixes, functionality extension, minor logic rewrites