cleanup and better output handling