Few function related fixes, optimization started