moved component ordering to a separate function and added edge case for empty boxes