Fix and improve code