added validation and cleanup