Removed while loops. Fixed exceptions.