updated code, alonalong with corrections from sir!