fix errors in the previous implementation