Some fixes on forking

Forking doesn't seem to work yet but this commit is still step forward
in making fork() work correctly. It also fixes some bugs related to the
sched/thread functions and removes unnecessary variables.