Working python solution, but it needs to be rewritten