Fix bug and improve code.