code cleanup and errorhandling