uva 784 modified to make it more concise