Added setting margin mask to allow for different margin order.
Cleaned unused variables and functions based on static analysis.