added logging and finalised code