Updated and debugged