Working version on CPU. Debugged