added download function and cleanup