clean up code and fix rand