tidy code and fix speed

Just cleans up the code a bit & fixes the speed change.