improve code readability

Current code is confusing, as embarassingly proved by #1037