housekeeping

better util functions, many minor refactorings