problem 4 solved, and some stylistic optimizations made