Solved ex-12-4-2 in a not so elegant manner. Need to work on it