Re-arrange code to reduce ifdef