fix reference errors; fix the logic of compilation
add test case