Refactored to enable pipelining.