slightly reorders code for clarity