Util functions and cleanup