edited brief solution slightly