EDIT: Optimization added.