bugfixed and optimised!