partially debugged version