fixed and slightly improve code