Many small improvements, optimization and fixes