Consolidation of logic. Adding test cases.