fixed a bug with nulls and made the code more readable