updated code with C+T's changes