Improved the speed a lot and fixed #3