Finished initial implementation for memory constraint.