Added better error handling of corner case conditions