Moved projection code to a function