Remove some duplicate processing and simplify the code a bit