some bugfixes and code optimisation