Simple and dirty functional implementation of first problem