Speed improvements

Check the commits for more information.