add more comments and improve code styling a bit