Removed unneeded intermediate forms and fixed order of operations