implement session and improve code