re-write to perform better with less memory