Cleaned-up code according to remarks made.