Fix bug.
Refactor a bit.