Added lots of helper functions for optimization.