tidied up code; added levels