added inelegant solution and fixed bug in the elegant solution