updated to beej's version