wrote test and speed improvements