Some code simplifications and readability improved.