Fixes a bug a optimizes some routines.