beautify code + callbacks for open and close