some cleanup, more input validation