Further changes and improvements, still not tested
further testing needed