Minor optimizations, minor bugfixes
resutls still to be checked against python