add some better handling
Move some stuff into util