code refactored alot
little permutation coding