adds some optimisation and minor bugfixes
merges changes, adds minor changes