Tested and debugged version