Various bugfix and improvements