lab1 SP code improved