Added memory stage and cleaned up circuit