Fixed and finished the current code for v3.