Added some speedups to the code