rewrote functions to use less memory