Tried to simplify the code to make it easier to read
Further testing and testability