Working solution. Ugliest C++ ever created