cleanup function, some fixes