major cleanup and improved block handling