Very ugly solution - needs heavy refactoring to be able to solve much faster