finalised program. in need of elegantifying