improved functional code for clarity