Make the code a bit sane, add comments

Signed-off-by: Thameera Senanayaka <thameera123@gmail.com>