close to debugged version