Working
Also working
Split monolithic function into more managable functions and eleminated code duplication