cleanup and rand improvements