finalised code