Clean up code handling a bit