Added speed optimization tweaks as inspired by Totorom's recent commit.

Change-Id: I042ea5466a89133b4ba0d188f070169042cda8e4